package com.neusoft.ecommerce.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.neusoft.ecommerce.dbhelper.LayuiPager;
import com.neusoft.ecommerce.entity.AfterSaleService;
import com.neusoft.ecommerce.entity.Goods;
import com.neusoft.ecommerce.entity.Indent;
import com.neusoft.ecommerce.entity.IndentDetail;
import com.neusoft.ecommerce.service.GoodsService;
import com.neusoft.ecommerce.service.IndentDetailService;
import com.neusoft.ecommerce.service.IndentService;

/**
 * 
 * @author YinKuenTam
 *
 */
@RestController
public class IndentDetailController {
	private final IndentDetailService indentDetailService;
	private final IndentService indentService;
	private final GoodsService goodsService;
	
	public IndentDetailController(IndentDetailService indentDetailService,IndentService indentService,GoodsService goodsService) {
		this.indentDetailService = indentDetailService;
		this.indentService = indentService;
		this.goodsService = goodsService;
	}
	@GetMapping("/indentDetail_page")
    public ModelAndView storagePage() {
        return new ModelAndView("indentDetail/indentDetail-list");
    }
	

    @RequestMapping("/indentDetail/list")
    @ResponseBody
    public LayuiPager<IndentDetail> list(@RequestParam(value = "page", defaultValue = "1") String page,
                                    @RequestParam(value = "limit", defaultValue = "10") String limit) {
        System.out.println(page + "" + limit);
        LayuiPager<IndentDetail> pager = new LayuiPager<>(Integer.parseInt(page), Integer.parseInt(limit));
        int count = indentDetailService.findTotal(pager);
        List<IndentDetail> list = indentDetailService.findList(pager);
        for (IndentDetail storage : list) {
            System.out.println(storage);
        }
        pager.setData(list);
        pager.setCount(count);
        pager.setCode(0);
        pager.setMsg("");
        return pager;
    }
    @GetMapping("/addIndentDetail")
    public ModelAndView addStorage() {
        return new ModelAndView("indentDetail/indentDetail-add");
    }
    @RequestMapping("/indentDetail/indentList")
    public List<Indent> findIndentList() {
	    return indentService.findAll();
    }
    @RequestMapping("/indentDetail/goodsList")
    public List<Goods> findGoodsList() {
	    return goodsService.findAll();
    }
    
    
    @PostMapping("/indentDetail/add")
    public Map<String, Object> add(IndentDetail indentDetail) {
        System.out.println(indentDetail);
        Map<String, Object> map = new HashMap<>(16);
        if (indentDetailService.add(indentDetail)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
    
    @GetMapping("/indentDetail/edit_page")
    public ModelAndView editPage(IndentDetail indentDetail, Model model) {
        System.out.println(indentDetail);
        LayuiPager<IndentDetail> pager = new LayuiPager<>(1,1);
        pager.setCondition(indentDetail);
        IndentDetail message = indentDetailService.findList(pager).get(0);
        System.out.println(message);
        ModelAndView view = new ModelAndView("indentDetail/indentDetail-edit");
        model.addAttribute("indentDetail", message);
        return view;
    }


	@PostMapping("/indentDetail/edit")
    public Map<String, Object> edit(IndentDetail indentDetail) {
        System.out.println(indentDetail);
        Map<String, Object> map = new HashMap<>(16);
        if (indentDetailService.edit(indentDetail)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	@PostMapping("/indentDetail/delete")
    public Map<String, Object> delete(IndentDetail indentDetail) {
        System.out.println(indentDetail);
        Map<String, Object> map = new HashMap<>(16);
        if (indentDetailService.delete(indentDetail)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
    
    

}
